const { app, Notification, Tray, Menu } = require("electron")
const { LOGO_TITLE, LOGO_EN } = require("../config/index")

let notification = null
let tray = null
let win = null
// 桌面通知
const notificationfn = (icon) => {
  app.setAppUserModelId(LOGO_EN)
  if (!Notification.isSupported()) return Promise.reject("当前系统不支持通知")
  notification = new Notification({
    title: LOGO_TITLE,
    body: '数据上传下载工具',
    icon: icon,
    silent: false, // 是否静音
    sound: true, // 是否播放声音
    urgency: "normal", // 重要性
    timeoutType: "default",
  })
  notification.show()
}
// 系统托盘
const trayfn = (icon, win1) => {
  win = win1
  tray = new Tray(icon)
  const contextMenu = Menu.buildFromTemplate([
    {
      label: '打开',
      click: () => {
        win.show()
      }
    },
    {
      label: '隐藏',
      click: () => {
        win.hide()
      }
    },
    {
      label: '退出',
      click: () => {
        app.quit();
      },
    }
  ])
  tray.on('click', () => {
    win.show()
  })
  tray.setToolTip(LOGO_TITLE)
  tray.setContextMenu(contextMenu)
}
// 销毁
const closeUtils = () => {
  notification && notification.close()
  tray && tray.isDestroyed() && tray.destroy()
}

module.exports = {
  notificationfn,
  trayfn,
  closeUtils
}